package com.example.ktproject

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var keyboardUtil: KeyboardUtil

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 默认选中第三个控件
        keyboardUtil = KeyboardUtil(this)
        keyboardUtil.hideSoftInputMethod(et2)
        keyboardUtil.showKeyboard()
        keyboardUtil.setEditText(et2)
        keyboardUtil.changeKeyboard(true)
        et2.setBackgroundResource(R.drawable.shape_et_select)
        et2.requestFocus()

        val etList = listOf(etProvince, et1, et2, et3, et4, et5, et6, et7)

        etProvince.setOnTouchListener(MyOnTouchListener(0, false, etList, keyboardUtil))
        et1.setOnTouchListener(MyOnTouchListener(1, true, etList, keyboardUtil))
        et2.setOnTouchListener(MyOnTouchListener(2, true, etList, keyboardUtil))
        et3.setOnTouchListener(MyOnTouchListener(3, true, etList, keyboardUtil))
        et4.setOnTouchListener(MyOnTouchListener(4, true, etList, keyboardUtil))
        et5.setOnTouchListener(MyOnTouchListener(5, true, etList, keyboardUtil))
        et6.setOnTouchListener(MyOnTouchListener(6, true, etList, keyboardUtil))
        et7.setOnTouchListener(MyOnTouchListener(7, true, etList, keyboardUtil))


        etProvince.addTextChangedListener(MyTextWatcher(etProvince, et1, true, keyboardUtil))
        et1.addTextChangedListener(MyTextWatcher(et1, et2, true, keyboardUtil))
        et2.addTextChangedListener(MyTextWatcher(et2, et3, true, keyboardUtil))
        et3.addTextChangedListener(MyTextWatcher(et3, et4, true, keyboardUtil))
        et4.addTextChangedListener(MyTextWatcher(et4, et5, true, keyboardUtil))
        et5.addTextChangedListener(MyTextWatcher(et5, et6, true, keyboardUtil))
    }

    class MyTextWatcher(
        private val curEditText: EditText,
        private val nextEditText: EditText,
        private val isNumber: Boolean,
        private val keyboardUtil: KeyboardUtil
    ) : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable?) {
            s?.let {
                if (it.isNotEmpty()) {
                    // 下个控件获取焦点
                    nextEditText.requestFocus()
                    nextEditText.setSelection(nextEditText.text.length)
                    // 更新背景
                    curEditText.setBackgroundResource(R.drawable.shape_et)
                    nextEditText.setBackgroundResource(R.drawable.shape_et_select)
                    // 切换键盘
                    keyboardUtil.changeKeyboard(isNumber)
                    keyboardUtil.setEditText(nextEditText)
                }
            }
        }

    }

    class MyOnTouchListener(
        private val index: Int,
        private val isNumber: Boolean,
        private val etList: List<EditText>,
        private val keyboardUtil: KeyboardUtil
    ) : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            changeEditTextBg(index, etList)
            // 切换键盘
            keyboardUtil.hideSoftInputMethod(v as EditText)
            keyboardUtil.changeKeyboard(isNumber)
            keyboardUtil.setEditText(v)
            v.setSelection(v.text.length)

            return false
        }

        // 更新控件背景
        private fun changeEditTextBg(index: Int, etList: List<EditText>) {
            etList[0].setBackgroundResource((if (index == 0) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[1].setBackgroundResource((if (index == 1) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[2].setBackgroundResource((if (index == 2) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[3].setBackgroundResource((if (index == 3) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[4].setBackgroundResource((if (index == 4) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[5].setBackgroundResource((if (index == 5) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[6].setBackgroundResource((if (index == 6) R.drawable.shape_et_select else R.drawable.shape_et))
            etList[7].setBackgroundResource((if (index == 7) R.drawable.shape_et_select else R.drawable.shape_et_green))
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (keyboardUtil.isShow()) {
                keyboardUtil.hideKeyboard()
            } else {
                finish()
            }
        }
        return false
    }
}
